package com.flmes.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flmes.base.base.exception.BusinessException;
import com.flmes.base.base.result.ResultCode;
import com.flmes.base.model.domain.User;
import com.flmes.base.model.mapper.UserMapper;
import com.flmes.base.service.UserService;
import com.flmes.base.util.BcryptPasswordUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: flmes
 * @Date: 2024/9/23 19:46
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public String login(String username, String password) {
        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        // 账号不存在或密码错误
        if (Objects.isNull(user)) {
            throw new BusinessException(ResultCode.ACCOUNT_NOT_FOUND);
        }
        if (!BcryptPasswordUtil.checkPw(password, user.getPassword())) {
            throw new BusinessException(ResultCode.PASSWORD_ERROR);
        }
        // TODO 生成token并返回
        return null;
    }

}
